package com.toobigtofail.entities
{
	import Box2D.Dynamics.b2Fixture;
	
	import com.toobigtofail.images.ShapeImage;
	import com.toobigtofail.shapes.IShape;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.Box2DShapeBuilder;
	import fabulaexmachina.box2fp.graphics.SuperGraphiclist;
	
	public class Target extends Box2DEntity
	{
		public static const TYPE:String = "target";
		private var _shape:IShape;
		private var _initOptions:Object;
		private var _image:ShapeImage;
		
		public function Target(shape:IShape, angle:Number=0, image:ShapeImage=null)
		{
			_shape = shape;
			super(shape.X, shape.Y, shape.Width, shape.Height,shape.b2Type);
			type = Target.TYPE;
			_initOptions = {angle: angle};
			
			if (image) {
				_image = image;
				_image.type = "Target";
				_image.angle = 0;
				_image.offsetForBox2D(Width, Height);
				graphic = new SuperGraphiclist(_image);
			}
		}
		
		override public function buildShapes(friction:Number, 
											 density:Number, restitution:Number,
											 group:int, category:int, collmask:int):void { 
			_shape.buildShape(body);
			this.angle = _initOptions.angle;
		}
		
		override public function update():void {
			super.update();
			
			if (_image) {
				_image.centerOrigin();
			}
		}
		
		public function serialize():XML {
			var t:XML = <target/>;
			if (_image) {
				var i:XML = <image/>;
				i.@shape = _image.shape;
				t.appendChild(i);
			}
			t.appendChild(_shape.serialize());
			
			return t;
		}
		
		public function get shape():IShape { return _shape; }
		public function get image():ShapeImage { return _image; }
	}
}